<template>
  <div class="material-detail-container">
    <div class="detail-header">
      <h2>材料详情</h2>
      <div class="header-actions">
        <button class="back-btn" @click="goBack">
          <span>返回</span>
        </button>
      </div>
    </div>

    <div class="material-info">
      <div class="info-row">
        <span class="info-label">材料名称:</span>
        <span class="info-value">{{ material.name }}</span>
      </div>
      <div class="info-row">
        <span class="info-label">文件类型:</span>
        <span class="info-value">{{ material.type }}</span>
      </div>
      <div class="info-row">
        <span class="info-label">上传时间:</span>
        <span class="info-value">{{ material.uploadTime }}</span>
      </div>
      <div class="info-row">
        <span class="info-label">材料大小:</span>
        <span class="info-value">{{ material.size }}</span>
      </div>
      <div class="info-row">
        <span class="info-label">关联人员:</span>
        <span class="info-value">{{ material.relatedPerson }}</span>
      </div>
    </div>

    <div class="preview-section">
      <h3>预览</h3>
      <div class="preview-content">
        <div v-if="isImage" class="image-preview">
          <img :src="material.previewUrl" :alt="material.name" />
        </div>
        <div v-else-if="isPDF" class="pdf-preview">
          <iframe :src="material.previewUrl" frameborder="0"></iframe>
        </div>
        <div v-else class="unsupported-preview">
          <p>不支持在线预览此文件类型</p>
          <p>请下载后查看</p>
        </div>
      </div>
    </div>

    <div class="action-buttons">
      <button class="download-btn" @click="downloadMaterial">
        <span>下载材料</span>
      </button>
      <button class="share-btn" @click="shareMaterial">
        <span>分享链接</span>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();

// 模拟材料数据
const material = ref({
  id: '',
  name: '教师资格证.pdf',
  type: 'PDF',
  uploadTime: '2023-06-15 14:30:22',
  size: '2.5 MB',
  relatedPerson: '张老师 (工号: 10001)',
  previewUrl: 'https://example.com/documents/teacher-certificate.pdf'
});

const isImage = computed(() => {
  const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp'];
  return imageExtensions.some(ext => material.value.name.toLowerCase().endsWith(ext));
});

const isPDF = computed(() => {
  return material.value.name.toLowerCase().endsWith('.pdf');
});

onMounted(() => {
  // 从路由参数中获取材料ID
  const materialId = route.params.id;
  if (materialId) {
    // 在实际应用中，这里会调用API获取材料详情
    material.value.id = materialId as string;
    console.log(`加载材料ID: ${materialId}`);
  }
});

function downloadMaterial() {
  alert(`开始下载: ${material.value.name}`);
  // 实际应用中这里会有下载逻辑
}

function shareMaterial() {
  alert(`生成分享链接: ${material.value.name}`);
  // 实际应用中这里会有分享逻辑
}

function goBack() {
  router.go(-1);
}
</script>

<style scoped>
.material-detail-container {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eaeaea;
}

.detail-header h2 {
  margin: 0;
  color: #1a2b4b;
  font-size: 20px;
  font-weight: 600;
}

.back-btn {
  padding: 8px 16px;
  background-color: #f0f2f5;
  color: #1a2b4b;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.back-btn:hover {
  background-color: #e2e8f0;
}

.material-info {
  margin-bottom: 30px;
  padding: 20px;
  background-color: #f8fafc;
  border-radius: 8px;
}

.info-row {
  display: flex;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eaeaea;
}

.info-row:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.info-label {
  flex: 0 0 120px;
  font-weight: 600;
  color: #555;
}

.info-value {
  flex: 1;
  color: #1a2b4b;
}

.preview-section {
  margin-bottom: 25px;
}

.preview-section h3 {
  margin: 0 0 15px 0;
  color: #1a2b4b;
  font-size: 18px;
  font-weight: 600;
}

.preview-content {
  border: 1px solid #eaeaea;
  border-radius: 8px;
  overflow: hidden;
  height: 500px;
}

.image-preview {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
}

.image-preview img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.pdf-preview {
  width: 100%;
  height: 100%;
}

.pdf-preview iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.unsupported-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background-color: #f5f7fa;
  color: #666;
  font-size: 18px;
}

.unsupported-preview p {
  margin: 10px 0;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.download-btn, .share-btn {
  padding: 12px 30px;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
}

.download-btn {
  background-color: #1a3a6c;
  color: white;
}

.download-btn:hover {
  background-color: #2c5282;
}

.share-btn {
  background-color: #f0f9eb;
  color: #67c23a;
  border: 1px solid #67c23a;
}

.share-btn:hover {
  background-color: #e1f3d8;
}
</style>